package com.javastar.common.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

@Configuration
@EnableSwagger2WebMvc
@AllArgsConstructor
public class SwaggerConfig {

    private final OpenApiExtensionResolver openApiExtensionResolver;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildExtensions("Novel"))
                .directModelSubstitute(java.util.Date.class, String.class)
                .securitySchemes(security());
//                .globalOperationParameters(setHeaderToken());
    }

//    /**
//     * 添加请求参数，我们这里把Accept-Language作为请求头部参数传入后端
//     */
//    private List<Parameter> setHeaderToken() {
//        ParameterBuilder languagePar = new ParameterBuilder();
//        List<Parameter> pars = new ArrayList<>();
//        languagePar.name("Accept-Language").description("语言设置:英文en_US;简体中文zh_CN;繁体中文zh_TW;").modelRef(new ModelRef("string")).parameterType("header").required(false).defaultValue("zh_CN").build();
//        pars.add(languagePar.build());
//        return pars;
//    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("novel网站项目").description("Novel-api模块接口文档")
                .termsOfServiceUrl("https://java-star.coding.net/p/novel-web").version("0.0.1.x").build();
    }

//    private List<Parameter> parameter() {
//        List<Parameter> pars = new ArrayList<>();
//        ParameterBuilder parameterBuilder = new ParameterBuilder();
//        pars.add(parameterBuilder.build());
//        return pars;
//    }

    private List<ApiKey> security() {
        return newArrayList(new ApiKey("token", "token", "header"));
    }

}